package xyz.eden.cloud.common.core.util;


import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * AssertTool
 */
public class AssertUtils {


    /**
     * is Not Empty
     *
     * @param collection
     * @param e
     * @param <K>
     * @param <V>
     * @throws Exception
     */
    public static <K, V> void isNotEmpty(Map<K, V> collection, Exception e) throws Exception {
        doAssert(collection, MapUtils::isNotEmpty, e);
    }

    /**
     * is Not Empty
     *
     * @param collection
     * @param e
     * @param <T>
     * @throws Exception
     */
    public static <T> void isNotEmpty(Collection<T> collection, Exception e) throws Exception {
        doAssert(collection, CollectionUtils::isNotEmpty, e);
    }

    /**
     * Is True
     *
     * @param bool
     * @param e
     * @throws Exception
     */
    public static void isTrue(Boolean bool, Exception e) throws Exception {
        doAssert(bool, (bo) -> {
            return Objects.equals(bo, true);
        }, e);
    }

    /**
     * Is Not Empty
     *
     * @param str
     * @param e
     * @throws Exception
     */
    public static void isNotEmpty(String str, Exception e) throws Exception {
        doAssert(str, StringUtils::isNotEmpty, e);
    }

    /**
     * Is Not Blank
     *
     * @param str
     * @param e
     * @throws Exception
     */
    public static void isNotBlank(String str, Exception e) throws Exception {
        doAssert(str, StringUtils::isNotBlank, e);
    }

    /**
     * Not Null
     *
     * @param obj
     * @param e
     * @throws Exception
     */
    public static void notNull(Object obj, Exception e) throws Exception {
        doAssert(obj, Objects::nonNull, e);
    }

    public static <T> void doAssert(T data, AssertFunction<T> assertFunction, Exception e) throws Exception {
        if (assertFunction.doApply(data)) {
            return;
        }
        throw e;
    }
}
